#ifndef AODV_FORWARD_H
#define AODV_FORWARD_H

#include "aodv_types.h"
#include "aodv_routing_table.h"

// 数据包转发结果
typedef enum {
    FORWARD_SUCCESS = 0,
    FORWARD_NO_ROUTE = -1,
    FORWARD_SEND_FAILED = -2,
    FORWARD_INVALID_PACKET = -3,
    FORWARD_ROUTE_DISCOVERY_NEEDED = -3
} forward_result_t;

// 数据包类型
typedef enum {
    PACKET_TYPE_DATA = 1,
    PACKET_TYPE_CONTROL = 2
} packet_type_t;

// 数据包头部结构
typedef struct {
    uint32_t src_addr;      // 源地址
    uint32_t dest_addr;     // 目标地址
    uint32_t packet_id;     // 数据包ID
    uint16_t packet_type;   // 数据包类型
    uint16_t data_len;      // 数据长度
    uint32_t timestamp;     // 时间戳
} data_packet_header_t;

// 数据转发统计
typedef struct {
    uint32_t packets_sent;
    uint32_t packets_received;
    uint32_t packets_forwarded;
    uint32_t route_discovery_requests;
    uint32_t route_discovery_failures;
} forward_stats_t;

// 函数声明

// 初始化数据转发模块
void forward_init(forward_stats_t *stats);

// 发送数据包（集成路由发现）
forward_result_t forward_data_packet(int socket, uint32_t src_addr, uint32_t dest_addr,
                                   const void *data, size_t data_len,
                                   aodv_routing_table *routing_table,
                                   uint32_t *sequence_number, uint16_t *rreq_id);

// 处理接收到的数据包
forward_result_t process_received_packet(int socket, const void *packet, size_t packet_len,
                                       uint32_t source_ip, aodv_routing_table *routing_table,
                                       uint32_t local_ip, uint32_t *sequence_number, uint16_t *rreq_id);

// 检查是否需要路由发现
int needs_route_discovery(const aodv_routing_table *routing_table, uint32_t dest_addr);

// 执行路由发现
forward_result_t perform_route_discovery(int socket, uint32_t src_addr, uint32_t dest_addr,
                                       aodv_routing_table *routing_table,
                                       uint32_t *sequence_number, uint16_t *rreq_id);

// 转发数据包到下一跳
forward_result_t forward_packet_to_next_hop(int socket, const data_packet_header_t *header,
                                          const void *data, size_t data_len,
                                          const aodv_routing_table *routing_table);

// 生成数据包ID
uint32_t generate_packet_id(void);

// 获取转发统计
void get_forward_stats(const forward_stats_t *stats, char *buffer, size_t buffer_size);

// 清理转发模块
void forward_cleanup(void);

#endif // AODV_FORWARD_H